﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Model;
using WMS.Repository.IRepository;
using WMS.Service.IService;

namespace WMS.Service
{
    public class BaseServices<TEntity> : IBaseService<TEntity> where TEntity : class, IBaseModel, new()
    {
        protected readonly IBaseRepository<TEntity> _repository;
        protected readonly ILogger<BaseServices<TEntity>> _logger;

        public BaseServices(IBaseRepository<TEntity> repository, ILogger<BaseServices<TEntity>> logger)
        {
            _repository = repository;
            _logger = logger;
        }

        public async Task<TEntity> GetAsync(int id)
        {
            try
            {
                return await _repository.GetAsync(id);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"查询实体失败 - 类型: {typeof(TEntity).Name}, ID: {id}");
                throw;
            }
        }

        public async Task<List<TEntity>> QueryAsync()
        {
            try
            {
                return await _repository.QueryAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"查询实体列表失败 - 类型: {typeof(TEntity).Name}");
                throw;
            }
        }
    }
}